#include "EngineHeader.h"

#pragma warning(disable:4996)

namespace LSH
{

//------------------------------------------------------------------------
File::File():_pFileHandle(NULL)
{
    _fileFullPath.clear();
}

//------------------------------------------------------------------------
File::File(String& fileName)
{
    assert(!fileName.empty());
#ifdef UNICODE
    CM_TCHAR fullPath[MAX_PATH] = {0};
    _wfullpath(fullPath, fileName.c_str(), MAX_PATH);
    _fileFullPath.assign(fullPath);
#else
    CM_TCHAR fullPath[MAX_PATH] = {0};
    _fullpath(fullPath, fileName.c_str(), MAX_PATH);
#endif // UNICODE
    open(fileName);
    assert(_pFileHandle);
}

//------------------------------------------------------------------------
File::~File()
{
    if(_pFileHandle)
    {
        close();
    }
}

//------------------------------------------------------------------------
bool File::open(String& fileName)
{
    close();
    _fileFullPath.assign(fileName);
#ifdef UNICODE
    _pFileHandle = _wfopen(fileName.c_str(), _T("r+b"));
#else
    _pFileHandle = fopen(fileName.c_str(), _T("r+b"));
#endif // UNICODE
    return (_pFileHandle != NULL);
}

//------------------------------------------------------------------------
void File::close()
{
    if(_pFileHandle)
    {
        fclose(_pFileHandle);
    }
    _pFileHandle = 0;
}

//------------------------------------------------------------------------
int File::length() const 
{
    return filelength(_fileno(_pFileHandle));
 }

//------------------------------------------------------------------------
int File::position() const
{
    return ftell(_pFileHandle);
}

//------------------------------------------------------------------------
bool File::seekSet(int offset)
{
    return fseek(_pFileHandle, offset, SEEK_SET) == 0;
}

//------------------------------------------------------------------------
bool File::seekCur(int offset)
{
    return fseek(_pFileHandle, offset, SEEK_CUR) == 0;
}

//------------------------------------------------------------------------
bool File::seekEnd(int offset)
{
    return fseek(_pFileHandle, offset, SEEK_END) == 0;
}

//------------------------------------------------------------------------
bool File::endOfFile() const
{
    return feof(_pFileHandle) != 0;
}

//------------------------------------------------------------------------
char File::parseChar() const
{
    return fgetc(_pFileHandle);
}

//------------------------------------------------------------------------
short File::parseShort() const
{
    int integer = 0;
    bool ret = (fscanf(_pFileHandle, "%d", integer) == 1);
    assert(ret);
    return (integer & 65535);
}

//------------------------------------------------------------------------
int File::parseInt() const
{
    int integer;
    bool ret = (fscanf(_pFileHandle, "%d", &integer) == 1);
    assert(ret);
    return integer;
}

//------------------------------------------------------------------------
float File::parseFloat() const{
    float real;
    bool ret = (fscanf(_pFileHandle, "%f", &real) == 1);
    assert(ret);
    return real;
}

//------------------------------------------------------------------------
double File::parseDouble() const
{
    double real;
    bool ret = (fscanf(_pFileHandle, "%f", &real) == 1);
    assert(ret);
    return real;
}

//------------------------------------------------------------------------
unsigned int File::readString(char *stringBuffer) const
{
    unsigned int length = 0;
    while((*stringBuffer++ = parseChar()) != '\0')
    {
        length++;
    }
    return length;
}

//------------------------------------------------------------------------
int File::read(void *buffer, int size, int count) const
{
    return fread(buffer, size, count, _pFileHandle);
}

//------------------------------------------------------------------------
unsigned char File::readByte() const
{
    unsigned char c;

    read(&c, sizeof(c), 1);

    return c;
}

//------------------------------------------------------------------------
char File::readChar() const
{
    unsigned char c;

    read(&c, sizeof(c), 1);

    return c;
}

//------------------------------------------------------------------------
unsigned short File::readWord() const
{
    unsigned short s;

    read(&s, sizeof(s), 1);

    return s;

}

//------------------------------------------------------------------------
unsigned int File::readDWord() const
{
    unsigned int word;

    read(&word, sizeof(word), 1);

    return word;

}

//------------------------------------------------------------------------
float File::readFloat() const
{
    float real;

    read(&real, sizeof(real), 1);

    return real;
}

//------------------------------------------------------------------------
double File::readDouble() const
{
    double real;

    read(&real, sizeof(real), 1);

    return real;
}


} // LSH
